package com.tmsps.ne4Weixin.api;

import com.alibaba.fastjson.JSONObject;
import com.tmsps.ne4Weixin.beans.BaseBean;
import com.tmsps.ne4Weixin.config.WxConfig;
import com.tmsps.ne4Weixin.message.out.OutBaseMsg;
import com.tmsps.ne4Weixin.message.out.OutTextMsg;
import com.tmsps.ne4Weixin.utils.HttpClient;

/**
 * @author zhangwei 396033084@qq.com CustomAPI 发送客服消息
 */
public class CustomAPI extends BaseAPI {
	private final String SENDURL = BaseURL.concat("/cgi-bin/message/custom/send?access_token=%s");

	public CustomAPI(WxConfig config) {
		super(config);
	}

	String getSendMsgJson(OutBaseMsg msg) {
		if (msg instanceof OutTextMsg) {
			JSONObject textMsg = new JSONObject();
			textMsg.put("touser", msg.getToUserName());
			textMsg.put("msgtype", "text");
			JSONObject content = new JSONObject();
			content.put("content", ((OutTextMsg) msg).getContent());
			textMsg.put("text", content);
			return textMsg.toJSONString();
		}
		return null;
	}

	public BaseBean sendTextMsg(String openId, String content) {
		OutTextMsg otm = new OutTextMsg();
		otm.setToUserName(openId);
		otm.setContent(content);
		String result = HttpClient.postJson(String.format(SENDURL, config.getAccessToken()), getSendMsgJson(otm));
		return JSONObject.parseObject(result, BaseBean.class);
	}
}
